package com.niodata.md.rdb.crawler;

/**
 * RetrievalOptions.
 */
public class DataBaseRetrievalOptions {

  private String namePattern;

  private String[] nameExclusions = new String[0];

  private TableRetrievalOptions tableRetrievalOptions = TableRetrievalOptions.retrievalWithDataAndColumnInfo();

  /**
   * get retrieval options with exclusions of system catalogs.
   *
   * @param dbCategory database category
   * @return database retrieval options
   */
  public static DataBaseRetrievalOptions optionsWithOutSystemCatalogs(String dbCategory) {
    if (dbCategory.equalsIgnoreCase("mysql")) {
      return mysqlWithoutSystemCatalogs();
    }

    if (dbCategory.equalsIgnoreCase("postgresql")) {
      return postgresqlWithoutSystemCatalogs();
    }

    if (dbCategory.equalsIgnoreCase("doris")) {
      return dorisWithoutSystemCatalogs();
    }

    if (dbCategory.equalsIgnoreCase("mssql")) {
      return mssqlWithoutSystemCatalogs();
    }

    return null;
  }

  /**
   * mysql default options with exclusions of system catalogs.
   *
   * @return retrieval options
   */
  public static DataBaseRetrievalOptions mysqlWithoutSystemCatalogs() {
    return new DataBaseRetrievalOptions()
        .withNamePattern(".*")
        .withNameExclusions(new String[]{
            "information_schema",
            "mysql",
            "performance_schema",
            "sys"
        });
  }

  /**
   * postgresql default options with exclusions of system catalogs.
   *
   * @return retrieval options
   */
  public static DataBaseRetrievalOptions postgresqlWithoutSystemCatalogs() {
    return new DataBaseRetrievalOptions()
        .withNamePattern(".*")
        .withNameExclusions(new String[]{
            "postgres",
            "template1"
        });
  }

  /**
   * doris default options with exclusions of system catalogs.
   *
   * @return retrieval options
   */
  public static DataBaseRetrievalOptions dorisWithoutSystemCatalogs() {
    return new DataBaseRetrievalOptions()
        .withNamePattern(".*")
        .withNameExclusions(new String[]{
            "information_schema",
            "__internal_schema",
            "mysql"
        });
  }

  /**
   * mssql default options with exclusions of system catalogs.
   *
   * @return retrieval options
   */
  public static DataBaseRetrievalOptions mssqlWithoutSystemCatalogs() {
    return new DataBaseRetrievalOptions()
        .withNamePattern(".*")
        .withNameExclusions(new String[]{
            "master",
            "model",
            "msdb",
            "tempdb"
        });
  }

  /**
   * set table retrieval options.
   *
   * @param tableRetrievalOptions table retrieval options
   */
  public DataBaseRetrievalOptions withTableRetrievalOptions(TableRetrievalOptions tableRetrievalOptions) {
    this.tableRetrievalOptions = tableRetrievalOptions;
    return this;
  }

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~help methods to create default options for different databases

  /**
   * get name pattern.
   *
   * @return name pattern
   */
  public String namePattern() {
    return this.namePattern;
  }

  /**
   * get name exclusions.
   *
   * @return name exclusions
   */
  public String[] nameExclusions() {
    return this.nameExclusions;
  }

  /**
   * set name pattern.
   *
   * @param namePattern name pattern
   * @return retrieval options
   */
  public DataBaseRetrievalOptions withNamePattern(String namePattern) {
    this.namePattern = namePattern;
    return this;
  }

  /**
   * set name exclusions.
   *
   * @param nameExclusions name exclusions
   * @return retrieval options
   */
  public DataBaseRetrievalOptions withNameExclusions(String[] nameExclusions) {
    this.nameExclusions = nameExclusions;
    return this;
  }

  /**
   * get table retrieval options.
   *
   * @return table retrieval options
   */
  public TableRetrievalOptions getTableRetrievalOptions() {
    return tableRetrievalOptions;
  }
}

